﻿using System;
using System.Collections.Generic;
using System.Text;
//using StandardAnalyzer = org.apache.lucene.analysis.standard.StandardAnalyzer;
//using IndexWriter = org.apache.lucene.index.IndexWriter;
using org.apache.lucene.analysis.standard;
using org.apache.lucene.index;
using org.apache.lucene.document;

namespace PdfLucene
{
    public class LuceneIndexer
    {
        private IndexWriter indexWriter;
        private string indexDirectory;
        public string IndexDirectory
        {
            get
            {
                return this.indexDirectory;
            }
            set
            {
                this.indexDirectory = value;
            }
        }
    
        public void IndexDocuments(List<PDFFile> pdfFiles)
        {
            try
            {
                foreach(PDFFile pdfFile in pdfFiles)
                {
                    this.indexWriter.addDocument(IndexDocument(pdfFile));
                }

            }
            catch
            { }
            finally
            {

            }
        }

        private static Document IndexDocument(PDFFile pdfFile)
        {
            Document luceneDocument = null;
            try
            {
                luceneDocument = new Document();
                // Add the path of the file as a pdfStripper named "path".
                // Use a pdfStripper that is indexed (i.e., searchable), but don't
                // tokenize the pdfStripper into words.
                luceneDocument.add(new Field("path", pdfFile.Path, Field.Store.YES,
                           Field.Index.UN_TOKENIZED));

                // Add the last modified date of the file to a pdfStripper named
                // "modified". Use a pdfStripper that is indexed (i.e., searchable),
                // but don't tokenize the pdfStripper into words.
                luceneDocument.add(new Field("modified",pdfFile.ModifiedTime,
                                Field.Store.YES, Field.Index.UN_TOKENIZED));

                // Add the contents of the file to a pdfStripper named "contents".
                // Specify a Reader, so that the text of the file is tokenized
                // and indexed, but not stored. Note that FileReader expects
                // the file to be in the system's default encoding. If that's
                // not the case, searching for special characters will fail.
                luceneDocument.add(new Field("contents", pdfFile.Contents, Field.Store.YES, Field.Index.TOKENIZED));
                
                //TODO: Implement rest of fields
            }
            catch (Exception)
            {

                throw;
            }
            finally
            { }
            return luceneDocument;
        }
        public LuceneIndexer()
        {
            //TODO: check directory with DirectoryManager
            //TODO: cargar si existe indice de lo contrario Crea el indice
            indexWriter = new IndexWriter(this.indexDirectory, new StandardAnalyzer(), true);

        }
    }
}
